/*****************************************************
*
*               COPYRIGHT (c) 2017-2019 Hargic Corporation
*                         All Rights Reserved
*
* The source code contained or described herein and all documents
* related to the source code ("Material") are owned by Hargic
* Corporation or its licensors.  Title to the Material remains
* with Hargic Corporation or its suppliers and licensors.
*
* The Material is protected by worldwide copyright and trade secret
* laws and treaty provisions. No part of the Material may be used,
* copied, reproduced, modified, published, uploaded, posted, transmitted,
* distributed, or disclosed in any way except in accordance with the
* applicable license agreement.
*
* No license under any patent, copyright, trade secret or other
* intellectual property right is granted to or conferred upon you by
* disclosure or delivery of the Materials, either expressly, by
* implication, inducement, estoppel, except in accordance with the
* applicable license agreement.
*
* Unless otherwise agreed by Hargic in writing, you may not remove or
* alter this notice or any other notice embedded in Materials by Hargic
* or Hargic's suppliers or licensors in any way.
*
** Author: shengda.huang
** Date: 2019-04-07
*
*****************************************************/

/***** Include files ********************************/
#include <stdio.h>
#include "am23xx_i2c.h"

/***** Defines **************************************/

/***** Define structure *****************************/

/***** Define constant ******************************/

/***** Define global ********************************/
static am23xx_i2c_func_t pfunc =
{
    .i2c_write = NULL,
    .i2c_read  = NULL,
};

/***** Define Prototype of functions ****************/

/**
 * @brief
 * @param  None
 * @retval None
 */
void am23xx_register_i2c_func(am23xx_i2c_func_t *func)
{
    pfunc.i2c_write = func->i2c_write;
    pfunc.i2c_read  = func->i2c_read;
}

static unsigned short am23xx_crc16(unsigned char *ptr, unsigned char len)
{
    unsigned short crc = 0xFFFF;
    unsigned char i;

    while (len--)
    {
        crc ^= *ptr++;
        for (i = 0; i < 8; i++)
        {
            if (crc & 0x01)
            {
                crc>>=1;
                crc^=0xA001;
            }
            else
            {
                crc>>=1;
            }
        }
    }
    return crc;
}

int am23xx_read_temp_humi(unsigned short *temp, unsigned short *humi)
{
    if (   pfunc.i2c_write == NULL
        || pfunc.i2c_read == NULL)
        return -10;

    unsigned char wrbuf[3] = {0x03, 0x00, 0x04};
    unsigned char recv[8];

    int ret = pfunc.i2c_write(wrbuf, sizeof(wrbuf));
    if (ret != 0)
    {
        printf("%s: write failed %d\n", __func__, ret);
        return -11;
    }

    ret = pfunc.i2c_read(recv, sizeof(recv));
    if (ret != 0)
    {
        printf("%s: read failed %d\n", __func__, ret);
        return -12;
    }

    if (recv[0] != 0x03)
    {
        printf("%s: recv func code 0x%x mismatch\n", __func__, recv[0]);
        return -13;
    }

    if (recv[1] != 4)
    {
        printf("%s: recv register size %d err\n", __func__, recv[1]);
        return -14;
    }

    unsigned short recv_crc = *(unsigned short *)&recv[6];
    unsigned short calc_crc = am23xx_crc16(recv, 6);
    if (recv_crc != calc_crc)
    {
        printf("%s: recv crc 0x%x mismatch calc crc 0x%x\n", __func__, recv_crc, calc_crc);
        return -15;
    }

    *humi = recv[2] << 8 | recv[3];
    *temp = recv[4] << 8 | recv[5];
    return 0;
}

/* End of file */



